from django.views.generic import View
from django.http import JsonResponse, HttpResponse
from goods.models import Goods, GoodsCategory
from django.forms.models import model_to_dict
from django.db.models import Q
from django.core import serializers
import json


class GoodsListView(View):
    def get(self, request):
        """
        通过django的View实现商品列表页
        :param request:
        :return:
        """
        goods = GoodsCategory.objects.filter(Q(id=1) | Q(parent_category__parent_category_id=1)
                                             | Q(parent_category_id=1)).order_by('-id')
        json_data = serializers.serialize("json", goods)
        json_data = json.loads(json_data)
        return JsonResponse(json_data, safe=False)
